<?php
class image_plugin {
    private $image;
    private $type;
    private $mime;
    private $width = 0;
    private $height = 0;
    function load( $image_path ) {
        $ext = pathinfo( $image_path, PATHINFO_EXTENSION );
        switch( $ext ) {
            case 'jpg':
                $this -> image = imagecreatefromjpeg( $image_path );
                $this -> mime = image_type_to_mime_type(IMAGETYPE_JPEG);
            break;
            case 'png':
                $this -> image = imagecreatefrompng( $image_path );
                $this -> mime = image_type_to_mime_type(IMAGETYPE_PNG);
            break;
            case 'gif':
                $this -> image = imagecreatefromgif( $image_path );
                $this -> mime = image_type_to_mime_type(IMAGETYPE_GIF);
            break;
            default:
                return false;
        }
        $this -> type = $ext;
        $this -> width = imagesx( $this -> image );
        $this -> height = imagesy( $this -> image );
        return true;
    }
    
    function resize( $width, $height, $preserveRatio = true ) {
        if( !$this -> image )
            return false;
        
        $image = imagecreatetruecolor($width, $height);
        
        imagealphablending( $image, true );
        imagesavealpha( $image, true );
        imagefill( $image, 0, 0, imagecolorallocatealpha( $image, 0, 0, 0, 127 ) ); 
        
        $nwidth = $width;
        $nheight = $height;
        $offsetX = 0;
        $offsetY = 0;
        if( $preserveRatio ) {
            
            if( $this -> width > $this -> height ) {
            
                $nheight = round( $this -> height * ( $width / $this -> width ) );
                $offsetY = round( ($height - $nheight)/2 );
                
            }else {
            
                $nwidth = round( $this -> width * ( $height / $this -> height ) );
                $offsetX = round( ($width - $nwidth)/2 );
            
            }
            
        }
        
        if( !imagecopyresampled($image, $this -> image, $offsetX, $offsetY, 0, 0, $nwidth, $nheight, $this -> width, $this -> height) )
            return false;
        imagedestroy($this -> image);
        $this -> image = $image;
        
    }
    
    function rotate( $angle ) {
        if( !$this -> image )
            return false;
            
        $this -> image = imagerotate($this ->image, $angle, 0 );
        imagealphablending($this -> image, true);
        imagesavealpha($this -> image, true); 
    }
    
    //@todo function crop()
    
    function save( $path, $quality = 100 ) {
        $path = pathinfo( $path );
        $image_path = $path[ 'dirname' ] . '/' . $path[ 'filename' ];
        switch( $this -> type ) {
            case 'jpg':
                imagejpeg( $this -> image, $image_path . '.' . $this -> type, $quality );
            break;
            case 'png':
                imagepng( $this -> image, $image_path . '.' . $this -> type );
            break;
            case 'gif':
                imagegif( $this -> image, $image_path . '.' . $this -> type );
            break;
            default:
                return false;
        }
    }
    
    function show( $quality = 100 ) {
        header( 'Content-type: ' . $this -> mime );
        switch( $this -> type ) {
            case 'jpg':
                imagejpeg( $this -> image, null, $quality );
            break;
            case 'png':
                imagepng( $this -> image );
            break;
            case 'gif':
                imagegif( $this -> image );
            break;
            default:
                return false;
        }
    }
    
    function destroy() {
        imagedestroy( $this -> image );
        $this -> type = null;
        $this -> mime = null;
        $this -> width = 0;
        $this -> height = 0;
    }
    
    function generate_token( $word, $width = 80, $height = 30, $text_x, $text_y, $colorize = true, $ttf_font_file = null, $font_size = 14 ) {
        $pic = ImageCreate( $width, $height );
        
        $c_w = strlen( $word );
        
        ImageFill( $pic, 1, 1, ImageColorAllocate( $pic, 255, 255, 255 ) );
        
        for( $i=0; $i<200; $i++)
        {
            $x = rand( 0, $width );
            $y = rand( 0, $height );
            ImageLine( $pic, $x, $y, $x, $y, ImageColorAllocate( $pic, 150, 150, 150 ) );
        }
        
        if( $colorize ) {
            $colors = array(
                ImageColorAllocate( $pic, 255, 0, 0 ),//red
                ImageColorAllocate( $pic, 0, 175, 255 ),//blue
                ImageColorAllocate( $pic, 25, 190, 15 ),//green
                ImageColorAllocate( $pic, 250, 190, 0 ),//orange
                ImageColorAllocate( $pic, 0, 0, 0 ),//black
                ImageColorAllocate( $pic, 245, 0, 220 ),//pink
                ImageColorAllocate( $pic, 240, 225, 10 ),//yellow
            );
        }else
            $colors[] = ImageColorAllocate( $pic, 0, 0, 0 );
        $rand = count( $colors ) - 1;
        
        if( $ttf_font_file !== null ) {
            for( $i=0; $i < $c_w; $i++)
            {
                ImageTtfText( $pic, $font_size, 0, ($i*$text_x), $text_y, $colors[ rand( 0, $rand ) ], $ttf_font_file, $word[ $i ] );
            }
        }else {
            for( $i=0; $i < $c_w; $i++)
            {
                ImageString( $pic, $font, $text_x+($i*10+7), $text_y, $word[ $i ], $colors[ rand( 0, $rand ) ] );
            }
        }
        
        header( "Content-type: image/gif" );
        ImageGIF( $pic );
        return $word;
    }
}